//
//  LS_StatusOriginalView.swift
//  weibo
//
//  Created by 罗李苏 on 16/5/24.
//  Copyright © 2016年 Apple. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage
//设置对齐的边距
let statueCellMargin = 8
class LS_StatusOriginalView: UIView {
    //记录原创微博底部约束的属性
    var bottomCons:Constraint?

    var statusViewModel: LS_StatueViewModel? {
        didSet{
            //绑定数据
//            let url = NSURL(string: statusViewModel?.status?.user?.profile_image_url ?? "")
            headIcon.sd_setImageWithURL(statusViewModel?.headIconURL)
            nameLabel.text = statusViewModel?.status?.user?.name
            timeLabel.text = statusViewModel?.time
            sourceLabel.text = statusViewModel?.status?.text
            contentLabel.text = statusViewModel?.status?.text
            //认证图片
            verifiedImageView.image = statusViewModel?.verifiedImage
            //用户等级
            mbrankImageView.image = statusViewModel?.mbrankImage
            //给配图视图数据源赋值
//            pictureView.pictures = statusViewModel?.status?.pic_urls
            //在设置数据源之前根据图片的数量 来判断是否需要调整 '自己的底部'约束
            //在更新约束之前需要卸载之前记录的约束
            bottomCons?.uninstall()
            if let urls = statusViewModel?.status?.pic_urls where urls.count > 0 {
                //有配图的原创微博
                //设置数据
                pictureView.pictures = urls
                //修改底部约束
                self.snp_updateConstraints(closure: { (make) in
                    self.bottomCons = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
                })
                //显示配图视图
                pictureView.hidden = false
            } else {
                //没有配图的原创微博
                
                //设置数据
                pictureView.pictures = nil
                //修改约束
                self.snp_updateConstraints(closure: { (make) in
                    self.bottomCons = make.bottom.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin).constraint
                })
                //隐藏配图视图
                pictureView.hidden = true
            }

        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        
        addSubview(headIcon)
        addSubview(verifiedImageView)
        addSubview(mbrankImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(nameLabel)
        addSubview(contentLabel)
        //添加配图视图
        addSubview(pictureView)
        //添加分隔条
        addSubview(sepView)
        
        //设置约束
        sepView.snp_makeConstraints { (make) in
            make.top.left.right.equalTo(self)
            make.height.equalTo(StatusCellMargin)
            sepView.backgroundColor = UIColor.redColor()
        }
        headIcon.snp_makeConstraints { (make) in
            make.top.equalTo(sepView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(self).offset(StatusCellMargin)
            make.width.height.equalTo(40)
        }
        verifiedImageView.snp_makeConstraints { (make) in
            make.right.bottom.equalTo(headIcon)
        }
        nameLabel.snp_makeConstraints { (make) in
            make.top.equalTo(headIcon.snp_top)
            make.left.equalTo(headIcon.snp_right).offset(StatusCellMargin)
        }
        mbrankImageView.snp_makeConstraints { (make) in
            make.top.equalTo(headIcon.snp_top)
            make.left.equalTo(nameLabel.snp_right).offset(StatusCellMargin)

        }
        
        timeLabel.snp_makeConstraints { (make) in
            make.left.equalTo(headIcon.snp_right).offset(StatusCellMargin)
            make.bottom.equalTo(headIcon.snp_bottom)
        }
        
        sourceLabel.snp_makeConstraints { (make) in
            make.bottom.equalTo(headIcon.snp_bottom)
            make.left.equalTo(timeLabel.snp_right).offset(StatusCellMargin)
        }
        
        contentLabel.snp_makeConstraints { (make) in
            make.top.equalTo(headIcon.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(headIcon.snp_left)
        }
        pictureView.snp_makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(contentLabel.snp_left)
            //设置测试数据
//            make.width.height.equalTo(100)

        }
        
        //给原创微博底部添加约束
        self.snp_makeConstraints { (make) in
           self.bottomCons =  make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
        }
    }
    
    //懒加载
    private lazy var headIcon: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    //认证的头像
    private lazy var verifiedImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    private lazy var mbrankImageView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    private lazy var timeLabel: UILabel = UILabel(title: "11:11", font: 10, textColor: UIColor.orangeColor())
    private lazy var sourceLabel: UILabel = UILabel(title: "王大锤", font: 10, textColor: UIColor.darkGrayColor())
    private lazy var nameLabel: UILabel = UILabel(title: "杜雷", font: 14, textColor: UIColor.darkGrayColor())
    private lazy var contentLabel: UILabel = UILabel(title: "我是王大锤,我为自己带盐,我是王大锤,我为自己带盐我是王大锤,我为自己带盐我是王大锤,我为自己带盐我是王大锤,我为自己带盐我是王大锤,我为自己带盐我是王大锤,我为自己带盐我是王大锤,我为自己带盐", font: 14, textColor: UIColor.darkGrayColor(), alignment: .Left)
    //懒加载一个配图视图
    private lazy var pictureView:LS_StatusPictureView = LS_StatusPictureView()
    //懒加载分隔条
    private lazy var sepView: UIView = UIView()
}
